﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OAuth.Verifiers
{
    static class OAuthSessionParameterVerifier
    {
        /// <summary>
        /// 
        /// </summary>
        /// <remarks>
        /// For guidelines regarding the creation of new exception types, see
        ///    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconerrorraisinghandlingguidelines.asp
        /// and
        ///    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp07192001.asp    
        /// </remarks>
        [global::System.Serializable]
        public sealed class BadOAuthSessionParameterException : Exception
        {
            #region public BadOAuthSessionException()

            /// <summary>
            /// Initializes a new instance of the <see cref="T:BadOAuthSessionException"/> class
            /// </summary>
            public BadOAuthSessionParameterException()
            {
            }

            #endregion

            #region public BadOAuthSessionException(string message) : base(message)

            /// <summary>
            /// Initializes a new instance of the <see cref="T:BadOAuthSessionException"/> class
            /// </summary>
            /// <param name="message">A <see cref="T:System.String"/> that describes the error. The content of message is intended to be understood by humans. The caller of this constructor is required to ensure that this string has been localized for the current system culture.</param>
            public BadOAuthSessionParameterException(string message)
                : base(message)
            {
            }

            #endregion

            #region public BadOAuthSessionException(string message, Exception inner) : base(message, inner)

            /// <summary>
            /// Initializes a new instance of the <see cref="T:BadOAuthSessionException"/> class
            /// </summary>
            /// <param name="message">A <see cref="T:System.String"/> that describes the error. The content of message is intended to be understood by humans. The caller of this constructor is required to ensure that this string has been localized for the current system culture.</param>
            /// <param name="inner">The exception that is the cause of the current exception. If the innerException parameter is not a null reference, the current exception is raised in a catch block that handles the inner exception.</param>
            public BadOAuthSessionParameterException(string message, Exception inner)
                : base(message, inner)
            {
            }

            #endregion

        }

        public static void VerifyOAuthSession(IOAuthSessionParameter pSession)
        {
            if (pSession == null)
                throw new BadOAuthSessionParameterException("Session parameter object may not be null");

            VerifyConsumerKeyAndSecret(pSession);
            ServiceProviderVerifier.VerifyServiceProvider(pSession.ServiceProvider);
        }

        private static void VerifyConsumerKeyAndSecret(IOAuthSessionParameter pSession)
        {
            if (pSession.ConsumerKey == null || pSession.ConsumerSecret == null)
                throw new BadOAuthSessionParameterException("Consumer Key and Secret may not be null. May be empty string");
        }
    }
}
